#include "Component.hpp"
TTF_Font *Component::default_font = nullptr;
Component::~Component()
{
    for (Component *com : children)
    {
        delete com;
    }
    children.clear();
}

int Component::update(float delta)
{
    return 0;
}

void Component::render(SDL_Renderer *render)
{
    if(!visible){
        return ;
    }
    for (Component *com : children)
    {
        com->render(render);
    }
}

int Component::handleEvent(SDL_Event &evt)
{
    if(!valid){
        return 0;
    }
    for (Component *com : children)
    {
        com->handleEvent(evt);
    }
    if (evt.type == SDL_MOUSEMOTION)
    {
        if (Component::isInRect(this->abs_rect, evt.motion.x, evt.motion.y))
        {
            if (!this->inRect)
            {
                this->mouseEnter(evt.motion);
                this->inRect = true;
                return 1;
            }
            else
            {
                this->mouseHover(evt.motion);
                return 1;
            }
        }
        else if (this->inRect)
        {
            this->mouseLeave(evt.motion);
            this->inRect = false;
            return 1;
        }
    }
    else if (evt.type == SDL_MOUSEBUTTONDOWN && this->inRect)
    {
        if (evt.button.button == SDL_BUTTON_LEFT)
        {
            if (evt.button.clicks == 2)
            {
                this->mouseDbClick(evt.button);
                this->pressed = true;
                return 1;
            }
            else
            {
                this->mousePress(evt.button);
                this->pressed = true;
                return 1;
            }
        }
    }
    else if (evt.type == SDL_MOUSEBUTTONUP && this->inRect)
    {
        if (this->pressed)
        {
            this->mouseClick(evt.button);
            evt.button;
            this->pressed = false;
            this->mouseRelease(evt.button);
            return 1;
        }
    }
    else if (evt.type == SDL_KEYUP && hotkey == evt.key.keysym.sym)
    {
        return 1;
    }
    return 0;
}

void Component::setPosition(int x, int y)
{
    this->abs_rect.x = x;
    this->abs_rect.y = y;
}

const SDL_Rect &Component::getRect()
{
    return this->abs_rect;
}

void Component::setFont(TTF_Font *font)
{
    this->font = font;
}

void Component::setDefaultFont(TTF_Font *font)
{
    Component::default_font = font;
}

const TTF_Font *Component::getFont()
{
    return this->font == nullptr ? Component::default_font : this->font;
}

void Component::setSize(int w, int h)
{
    abs_rect.w = w;
    abs_rect.h = h;
    rect.w = w;
    rect.h = h;
}

void Component::setHotkey(char hotkey)
{
    if (hotkey < 'a')
    {
        this->hotkey = hotkey + ('a'-'A');
    }
    else
    {
        this->hotkey = hotkey;
    }
}

bool Component::isVisible()
{
    return visible;
}

bool Component::isValid()
{
    return valid;
}

void Component::setVisible(bool val)
{
    visible = val;
}

void Component::setValid(bool val)
{
    valid = val;
}

void Component::setIcon(std::string icon)
{
    current_icon = icon;
    this->icon = icon;
}

void Component::setIconHover(std::string hover)
{
    this->icon_hover = hover;
}

bool Component::isInRect(SDL_Rect &rc, int x, int y)
{
    if (x < rc.x || x >= rc.x + rc.w || y < rc.y || y >= rc.y + rc.h)
        return false;
    return true;
}

void Component::mouseHover(SDL_MouseMotionEvent &evt)
{
    state = ComponentState::HOVER;
}

void Component::mouseEnter(SDL_MouseMotionEvent &evt)
{
    state = ComponentState::HOVER;
}

void Component::mouseLeave(SDL_MouseMotionEvent &evt)
{
    state = ComponentState::NORMAL;
}

void Component::mouseClick(SDL_MouseButtonEvent &evt)
{
    state = ComponentState::PRESS;
}

void Component::mousePress(SDL_MouseButtonEvent &evt)
{
    state = ComponentState::PRESS;
}

void Component::mouseRelease(SDL_MouseButtonEvent &evt)
{
    state = ComponentState::HOVER;
}

void Component::mouseDbClick(SDL_MouseButtonEvent &evt)
{
    state = ComponentState::HOVER;
}

void Component::hotkeyEvent(SDL_KeyboardEvent &evt)
{
}
